package com.itydl.a03.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itydl.a03.R;
import com.itydl.a03.domain.Person;

import java.util.List;

/**
 * @author ydl
 * @version
 */
public class MyAdapter extends BaseAdapter {

    private List<Person> mPersonList;

    public MyAdapter(List<Person> personList) {

        mPersonList = personList;
    }

    @Override
    public int getCount() {
        return mPersonList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = View.inflate(parent.getContext(), R.layout.list_item,null);
        }

        TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
        TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);

        Person person = mPersonList.get(position);

        String pinyin = person.getPinyin();
        String currentLetter = pinyin.charAt(0)+"";
        int num = 0;//标记，1代表要显示tittle字母

        if(position == 0){
            num = 1;
        }else{
            //获取上一个item的首字母
            String preLetter = mPersonList.get(position - 1).getPinyin().charAt(0) + "";
            if(!TextUtils.equals(preLetter,currentLetter)){
                num = 1;
            }
        }
        tv_title.setVisibility(num == 1?View.VISIBLE:View.GONE);
        tv_title.setText(currentLetter);
        tv_name.setText(person.getName());

        return convertView;
    }
}
